/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 14, 2012  11:15:55 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : promis.utility
 * File Name : Utilitys.java
 * TODO
 */
package promis.utility;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import promis.db.connection.Userinfo;
import java.lang.reflect.*;
import java.nio.charset.Charset;

/**
 * @author chatree
 *
 */
public class Utilitys {
	
	public static final Charset UTF_32 = Charset.forName("UTF-32");
    public static final Charset utf_8 = Charset.forName("utf-8");
    public static final Charset iso_8859_1 = Charset.forName("ISO-8859-1");
    public static final Charset tis_620 = Charset.forName("TIS-620");

    public static String strSql(String strVal){
        if(strVal == null) return " NULL ";
        if(strVal.equals("")) return " NULL ";
        else {
            return "'" + strVal + "'";
        }
    }
    
    /**
     * CURRENT_DATE
     * @return
     */
    public static String oracleCurrentDate(){
        return " CURRENT_DATE ";
    }
    
    public static String CurrentUserSync(){
    	return Userinfo.getUser();
    }
    
    public static boolean IsNull(String strVal){
        if(strVal == null) 
            return true;
        else if(strVal.equals("")) 
            return true;
        else if(strVal.toUpperCase().equals("NULL")) 
            return true;
        else
            return false;
    }
    
    public static String IsNullRNull(String strVal){
        return IsNull(strVal) ? null : strVal;
    }
    
    public static String IsNullRZero(String strVal){
        return IsNull(strVal) ? "0" : strVal;
    }
    
    public static String IsNullRString(String strVal){
        return IsNull(strVal) ? "" : strVal;
    }
    
    public static String ThaiCharset(String txt_value)
    {
    	String temp = new String(txt_value.getBytes(),UTF_32);
		return temp;
    }
	
	/**
	 * @return the current_date
	 */
	public static Date getCurrent_date() {
		return Calendar.getInstance().getTime();
	}

	/**
	 * Input : 2012-02-13//01:01:01 [yyyy-MM-dd//HH:mm:ss]
	 * return : DateTimne Type; 
	 * @param str_date
	 * @return
	 * @throws ParseException
	 */
	public static Date ConvertStringToDateTime(String str_date) throws ParseException {
		
		if(str_date == null || str_date.equals(""))
			return null;
		
		java.text.DateFormat formatter ; 
		java.util.Date date ; 
		formatter = new java.text.SimpleDateFormat("yyyy-MM-dd//HH:mm:ss");
		date = (Date)formatter.parse(str_date);
		
		return date;
	}
	
	 /**
	   * The maximum size to which the padding constant(s) can expand.
	   */
	  private static final int PAD_LIMIT = 8192;



	  /**
	   * Left pad a String with a specified character.
	   *
	   * Pad to a size of <code>size</code>.
	   *
	   * <pre>
	   * StringUtils.leftPad(null, *, *)     = null
	   * StringUtils.leftPad("", 3, 'z')     = "zzz"
	   * StringUtils.leftPad("bat", 3, 'z')  = "bat"
	   * StringUtils.leftPad("bat", 5, 'z')  = "zzbat"
	   * StringUtils.leftPad("bat", 1, 'z')  = "bat"
	   * StringUtils.leftPad("bat", -1, 'z') = "bat"
	   * </pre>
	   *
	   * @param str  the String to pad out, may be null
	   * @param size  the size to pad to
	   * @param padChar  the character to pad with
	   * @return left padded String or original String if no padding is necessary,
	   *  <code>null</code> if null String input
	   * @since 2.0
	   */
	  public static String leftPad(String str, int size, char padChar) {
	      if (str == null) {
	          return null;
	      }
	      int pads = size - str.length();
	      if (pads <= 0) {
	          return str; // returns original String when possible
	      }
	      if (pads > PAD_LIMIT) {
	          return leftPad(str, size, String.valueOf(padChar));
	      }
	      return padding(pads, padChar).concat(str);
	  }

	  /**
	   * Left pad a String with a specified String.
	   *
	   * Pad to a size of <code>size</code>.
	   *
	   * <pre>
	   * StringUtils.leftPad(null, *, *)      = null
	   * StringUtils.leftPad("", 3, "z")      = "zzz"
	   * StringUtils.leftPad("bat", 3, "yz")  = "bat"
	   * StringUtils.leftPad("bat", 5, "yz")  = "yzbat"
	   * StringUtils.leftPad("bat", 8, "yz")  = "yzyzybat"
	   * StringUtils.leftPad("bat", 1, "yz")  = "bat"
	   * StringUtils.leftPad("bat", -1, "yz") = "bat"
	   * StringUtils.leftPad("bat", 5, null)  = "  bat"
	   * StringUtils.leftPad("bat", 5, "")    = "  bat"
	   * </pre>
	   *
	   * @param str  the String to pad out, may be null
	   * @param size  the size to pad to
	   * @param padStr  the String to pad with, null or empty treated as single space
	   * @return left padded String or original String if no padding is necessary,
	   *  <code>null</code> if null String input
	   */
	  public static String leftPad(String str, int size, String padStr) {
	      if (str == null) {
	          return null;
	      }
	      if (isEmpty(padStr)) {
	          padStr = " ";
	      }
	      int padLen = padStr.length();
	      int strLen = str.length();
	      int pads = size - strLen;
	      if (pads <= 0) {
	          return str; // returns original String when possible
	      }
	      if (padLen == 1 && pads <= PAD_LIMIT) {
	          return leftPad(str, size, padStr.charAt(0));
	      }

	      if (pads == padLen) {
	          return padStr.concat(str);
	      } else if (pads < padLen) {
	          return padStr.substring(0, pads).concat(str);
	      } else {
	          char[] padding = new char[pads];
	          char[] padChars = padStr.toCharArray();
	          for (int i = 0; i < pads; i++) {
	              padding[i] = padChars[i % padLen];
	          }
	          return new String(padding).concat(str);
	      }
	  }
	  /**
	   * Returns padding using the specified delimiter repeated
	   * to a given length.
	   *
	   * <pre>
	   * StringUtils.padding(0, 'e')  = ""
	   * StringUtils.padding(3, 'e')  = "eee"
	   * StringUtils.padding(-2, 'e') = IndexOutOfBoundsException
	   * </pre>
	   *
	   * Note: this method doesn't not support padding with
	   * <a href="http://www.unicode.org/glossary/#supplementary_character">Unicode Supplementary Characters</a>
	   * as they require a pair of <code>char</code>s to be represented.
	   * If you are needing to support full I18N of your applications
	   * consider using {@link #repeat(String, int)} instead. 
	   * 
	   *
	   * @param repeat  number of times to repeat delim
	   * @param padChar  character to repeat
	   * @return String with repeated character
	   * @throws IndexOutOfBoundsException if <code>repeat &lt; 0</code>
	   * @see #repeat(String, int)
	   */
	  private static String padding(int repeat, char padChar) throws IndexOutOfBoundsException {
	      if (repeat < 0) {
	          throw new IndexOutOfBoundsException("Cannot pad a negative amount: " + repeat);
	      }
	      final char[] buf = new char[repeat];
	      for (int i = 0; i < buf.length; i++) {
	          buf[i] = padChar;
	      }
	      return new String(buf);
	  }
	  // Empty checks
	  //-----------------------------------------------------------------------
	  /**
	   * Checks if a String is empty ("") or null.
	   *
	   * <pre>
	   * StringUtils.isEmpty(null)      = true
	   * StringUtils.isEmpty("")        = true
	   * StringUtils.isEmpty(" ")       = false
	   * StringUtils.isEmpty("bob")     = false
	   * StringUtils.isEmpty("  bob  ") = false
	   * </pre>
	   *
	   * NOTE: This method changed in Lang version 2.0.
	   * It no longer trims the String.
	   * That functionality is available in isBlank().
	   *
	   * @param str  the String to check, may be null
	   * @return <code>true</code> if the String is empty or null
	   */
	  public static boolean isEmpty(String str) {
	      return str == null || str.length() == 0;
	  }
	
	
}
